import matplotlib.pyplot as plt
import math

# 这两行代码解决 plt 中文显示的问题
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False

fig, ax = plt.subplots(figsize=(35, 8))
title = "DERF_DAILY ACC SCORE"
time_list = ['01-01', '01-06', '01-11', '01-16', '01-21', '01-26',
             '02-01', '02-06', '02-11', '02-16', '02-21', '02-26',
             '03-01', '03-06', '03-11', '03-16', '03-21', '03-26']
# 数值范围正负1，保留两位小数

acc_score_list = [[0.75, 0.32, -0.35, 0.09, -0.15, 0.08, -0.75, -0.32, 0.35, -0.03, -0.15, 0.88, 0.75, -0.32, -0.35, -0.07, -0.15, 0.88],
                  [-0.42, -0.42, -0.75, 0.49, 0.35, -0.18, 0.45, 0.82, -0.15, 0.33, 0.55, -0.28, 0.55, 0.39, -0.50, 0.17, 0.75, -0.48],
                  [0.77, 0.42, -0.15, -0.49, -0.55, -0.58, 0.25, -0.72, 0.85, 0.13, 0.17, -0.38, 0.76, 0.82, -0.25, 0.37, 0.45, -0.68],
                  [-0.35, 0.37, 0.35, 0.09, 0.15, -0.08, -0.75, -0.32, -0.35, -0.03, 0.15, 0.88, 0.85, -0.32, -0.35, -0.07, 0.15, 0.88],
                  [0.75, -0.32, -0.35, -0.09, -0.15, 0.08, 0.75, 0.32, 0.35, 0.03, -0.15, -0.88, 0.75, 0.12, 0.35, 0.07, -0.15, -0.88]]

score_length = len(acc_score_list)

labels = ['OSR', 'NBC', 'PPD', 'DT', 'SVM']
colors = ['#FF0000', '#FF7F00', '#FFFF00', '#00FF00', '#00FFFF']

width = 0.15

# 偶数的时候还需要看是否需要调试，奇数是OK的
center_index = math.floor(score_length / 2)
for score_index in range(score_length):
    acc_score = acc_score_list[score_index]
    acc_len = len(acc_score)
    x = list(range(acc_len))
    x = [i - (center_index - score_index) * width for i in x]
    plt.bar(x, acc_score, width=width, color=colors[score_index], label=labels[score_index])
    for i in range(acc_len):
        value = acc_score[i]
        index = x[i]
        if value != 0:
            if value > 0:
                plt.text(index - 0.09, value + 0.02, "%s" % value, va='center', size=8)
            else:
                plt.text(index - 0.13, value - 0.02, "%s" % value, va='center', size=8)
        else:
            plt.text(index - 0.09, value, "missing", va='center', size=8)

length = len(acc_score_list[0])
plt.title(title, size=20)
x = list(range(length))
plt.xticks(x, labels=time_list, fontsize=11)
plt.yticks(fontsize=11)

ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)

zero = [0 for i in range(length)]
ax.plot(time_list, zero, color="#cccccc")

save_path = ""
image_name = "multiHistogram.png"
plt.legend()
plt.savefig(save_path + image_name, bbox_inches='tight', transparent=True, pad_inches=0.5, facecolor='#FFFFFF')
plt.clf()
plt.close(fig)
